﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using Entity;

namespace Controller
{
    public class CustInvoiceController:ControllerBase
    {


        public int addCustInvoice(string stockOutNo, int stockOutId, int paid)
        {
            stockOutNo = stockOutNo.Trim();
            var count = (from invoice in Sdc.CustInvoices
                         where invoice.No == stockOutNo
                         select invoice).Count();
            if (count <= 0)
            {
                CustInvoice custInvoice = new CustInvoice();
                custInvoice.No = stockOutNo;
                custInvoice.StockOutId = stockOutId;
                custInvoice.Paid = (byte)paid;
                custInvoice.Remark = "";
                Sdc.CustInvoices.InsertOnSubmit(custInvoice);
                Sdc.SubmitChanges();
                return custInvoice.Id;
            }
            else
            {
                return -1;
            }
            
        }

        public IList<CustomerInvoiceEntity> FindAllCustomerInvoices()
        {
            IList<CustInvoice> custInvoices = (from c in Sdc.CustInvoices
                                               orderby c.StockOut.Date descending,c.StockOutId descending 
                                               select c).ToList();
            IList<CustomerInvoiceEntity> customerInvoiceEntities =new List<CustomerInvoiceEntity>();
            foreach (CustInvoice custInvoice in custInvoices)
            {
                CustomerInvoiceEntity customerInvoiceEntity = TransformCustInvoiceToCustomerInvoiceEntity(custInvoice);
                customerInvoiceEntities.Add(customerInvoiceEntity);
            }
            return customerInvoiceEntities;
        }

        public IList<CustomerInvoiceEntity> FindCustomerInvoiceByDate(DateTime fromDate, DateTime toDate)
        {
            IList<CustInvoice> custInvoices = (from c in Sdc.CustInvoices
                                               where c.StockOut.Date >= fromDate && c.StockOut.Date <= toDate
                                               orderby c.StockOut.Date descending, c.StockOutId descending 
                                               select c).ToList();
            IList<CustomerInvoiceEntity> customerInvoiceEntities = new List<CustomerInvoiceEntity>();
            foreach (CustInvoice custInvoice in custInvoices)
            {
                CustomerInvoiceEntity customerInvoiceEntity = TransformCustInvoiceToCustomerInvoiceEntity(custInvoice);
                customerInvoiceEntities.Add(customerInvoiceEntity);
            }
            return customerInvoiceEntities;
        }

        public IList<CustomerInvoiceEntity> FindCustomerInvoiceByInvoiceNo(string invoiceNo)
        {
            IList<CustInvoice> custInvoices = (from c in Sdc.CustInvoices
                                               where c.No.ToUpper().Contains(invoiceNo)
                                               orderby c.StockOut.Date descending, c.StockOutId descending 
                                               select c).ToList();
            IList<CustomerInvoiceEntity> customerInvoiceEntities = new List<CustomerInvoiceEntity>();
            foreach (CustInvoice custInvoice in custInvoices)
            {
                CustomerInvoiceEntity customerInvoiceEntity = TransformCustInvoiceToCustomerInvoiceEntity(custInvoice);
                customerInvoiceEntities.Add(customerInvoiceEntity);
            }
            return customerInvoiceEntities;
        }

        public IList<CustomerInvoiceEntity> FindCustomerInvoiceByCustomer(int customerId)
        {
            IList<CustInvoice> custInvoices = (from c in Sdc.CustInvoices
                                               where c.StockOut.CustId == customerId
                                               orderby c.StockOut.Date descending, c.StockOutId descending 
                                               select c).ToList();
            IList<CustomerInvoiceEntity> customerInvoiceEntities = new List<CustomerInvoiceEntity>();
            foreach (CustInvoice custInvoice in custInvoices)
            {
                CustomerInvoiceEntity customerInvoiceEntity = TransformCustInvoiceToCustomerInvoiceEntity(custInvoice);
                customerInvoiceEntities.Add(customerInvoiceEntity);
            }
            return customerInvoiceEntities;
        }

        public static CustomerInvoiceEntity TransformCustInvoiceToCustomerInvoiceEntity(CustInvoice custInvoice)
        {
            CustomerInvoiceEntity customerInvoiceEntity=new CustomerInvoiceEntity();
            customerInvoiceEntity.CustomerInvoiceId = custInvoice.Id;
            customerInvoiceEntity.CustomerInvoiceDate = custInvoice.StockOut.Date;
            customerInvoiceEntity.CustomerInvoiceNo = custInvoice.No;
            CustomerController customerController=new CustomerController();
            CustomerEntity customerEntity = customerController.FindCustomerByCustomerId(custInvoice.StockOut.CustId);
            
            customerInvoiceEntity.CustomerShortName = customerEntity.ShortName;
            customerInvoiceEntity.CustomerAddress = customerEntity.Address;
            customerInvoiceEntity.CustomerId = customerEntity.Id;

            if(custInvoice.Paid==1)
            {
                customerInvoiceEntity.Paid = "Yes";
            }
            if(custInvoice.Paid==0)
            {
                customerInvoiceEntity.Paid = "No";
            }
            return customerInvoiceEntity;
        }

        public CustomerInvoiceEntity FindCustomerInvoiceById(int customerInvoiceId)
        {
            CustInvoice custInvoice = (from c in Sdc.CustInvoices
                                       where c.Id == customerInvoiceId
                                       select c).SingleOrDefault();
            CustomerInvoiceEntity customerInvoiceEntity = TransformCustInvoiceToCustomerInvoiceEntity(custInvoice);
            return customerInvoiceEntity;
        }

        public int DeleteCustomerInvoiceById(int customerInvoiceId)
        {
            try
            {
                //Delete vendor invoice
                CustInvoice custInvoice = (from c in Sdc.CustInvoices
                                           where c.Id == customerInvoiceId
                                           select c).SingleOrDefault();
                Sdc.CustInvoices.DeleteOnSubmit(custInvoice);
                //Sdc.SubmitChanges();

                //Delete stock in
                StockOut stockOut = (from s in Sdc.StockOuts
                                     where s.Id == custInvoice.StockOutId
                                   select s).SingleOrDefault();
                Sdc.StockOuts.DeleteOnSubmit(stockOut);
                //Sdc.SubmitChanges();

                //Update storage
                IList<Delivery> deliveries = (from d in Sdc.Deliveries
                                           where d.StockOutId == stockOut.Id
                                           select d).ToList();
                foreach (Delivery delivery in deliveries)
                {
                    Sdc.Deliveries.DeleteOnSubmit(delivery);
                    //Delete stock item
                    int qty = delivery.Qty;
                    StockItem stockItem = (from si in Sdc.StockItems
                                           where si.Id == delivery.StockItemId
                                           select si).SingleOrDefault();
                    stockItem.Stock = stockItem.Stock + qty;

                    //Update customer po item and customer po
                    CustPOItem custPoItem = (from ci in Sdc.CustPOItems
                                             where ci.Id == delivery.CustPOItemId
                                             select ci).SingleOrDefault();
                    custPoItem.CompleteQty = custPoItem.CompleteQty - qty;
                    custPoItem.Status = "customer PO";

                    CustPO custPo = (from cp in Sdc.CustPOs
                                     where cp.Id == custPoItem.CustPOId
                                     select cp).SingleOrDefault();
                    custPo.Status = "new";

                    //Update product stock
                    Product product = (from p in Sdc.Products
                                       where p.Id == stockItem.ProductId
                                       select p).SingleOrDefault();
                    product.Stock = product.Stock + qty;
                    //Sdc.SubmitChanges();
                }

                Sdc.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    }
}
